﻿using System;
using System.Linq;
using System.Collections.Generic;
using Light.Framework.Model;
using Light.Framework.Castle;
using NHibernate.Criterion;
using Light.Framework.Contract;
using Castle.ActiveRecord;
using System.Security.Permissions;
using System.Text;
using System.Data;
using Light.Utility;
using System.Data.Common;

namespace Light.Framework.Component
{
    public class DictionaryComponent : IDictionaryComponent
    {

        #region IDictionaryComponent 成员

        //Order[] orders = new Order[] { new Order("DisplayIndex", true) };
        //ICriterion criterion = Expression.Sql("1=1");

        public T GetByCode<T>(string code) where T : class
        {
            return ActiveRecordMediator<T>.FindFirst(Expression.Eq("Code", code));
        }

        public T GetByName<T>(string name) where T : class
        {
            return ActiveRecordMediator<T>.FindFirst(Expression.Eq("Name", name));
        }

        public IList<T> Get<T>() where T : class
        {
            string key = "DIC_" + typeof(T).Name;
            IList<T> models = ServerApplication.Cache.Get(key) as IList<T>;
            if (models == null)
            {
                ModelOrder order = new ModelOrder();
                order.Add("DisplayIndex", true);

                T[] results = ActiveRecordMediator<T>.FindAll(order.Orders);
                if (results != null)
                {
                    models = results.ToList();
                    ServerApplication.Cache.Add(key, models);
                }
            }
            return models;

        }

        public IList<T> Get<T>(SearchArgs args) where T : class
        {
            DetachedCriteria dc = DetachedCriteria.For<T>();
            if (args != null)
            {
                dc = args.ToDetachedCriteria<T>();
            }

            T[] results = ActiveRecordMediator<T>.FindAll(dc);
            if (results != null)
                return results.ToList();
            else
                return null;
        }

        public IList<City> GetCity(string provinceName)
        {

            string key = string.Format("DIC_{0}_CITY", provinceName);
            IList<City> models = ServerApplication.Cache.Get(key) as IList<City>;
            if (models == null)
            {
                ModelOrder order = new ModelOrder();
                order.Add("DisplayIndex", true);

                SearchArgs args = new SearchArgs();
                args.Add(SearchLogic.And, "Province.Name", provinceName);
                DetachedCriteria dc = args.ToDetachedCriteria<City>();

                City[] results = ActiveRecordMediator<City>.FindAll(dc, order.Orders);
                if (results != null)
                {
                    models = results.ToArray();
                }
                ServerApplication.Cache.Add(key, models);
            }
            return models;
        }

        public IList<District> GetDistrict(string cityCode)
        {

            string key = string.Format("DIC_{0}_DISTRICT", cityCode);
            IList<District> models = ServerApplication.Cache.Get(key) as IList<District>;
            if (models == null)
            {
                ModelOrder order = new ModelOrder();
                order.Add("DisplayIndex", true);

                SearchArgs args = new SearchArgs();
                args.Add(SearchLogic.And, "City.Code", cityCode);
                DetachedCriteria dc = args.ToDetachedCriteria<District>();

                District[] results = ActiveRecordMediator<District>.FindAll(dc, order.Orders);
                if (results != null)
                {
                    models = results.ToArray();
                }
                ServerApplication.Cache.Add(key, models);
            }
            return models;
        }

        public IList<District> GetDistrictByCityName(string cityName)
        {

            string key = string.Format("DIC_{0}_DISTRICT", cityName);
            IList<District> models = ServerApplication.Cache.Get(key) as IList<District>;
            if (models == null)
            {
                SearchArgs args = new SearchArgs();
                args.Add(SearchLogic.And, "City.Name", cityName);
                DetachedCriteria dc = args.ToDetachedCriteria<District>();

                District[] results = ActiveRecordMediator<District>.FindAll(dc);
                if (results != null)
                {
                    models = results.ToArray();
                }
                ServerApplication.Cache.Add(key, models);
            }
            return models;
        }

        #endregion
    }
}
